package org.budo.dubbo.protocol.http.view.render.impl;

import org.budo.dubbo.protocol.http.Consts;
import org.budo.dubbo.protocol.http.invocation.adapter.ProtocolRequest;
import org.budo.dubbo.protocol.http.invocation.adapter.ProtocolResponse;
import org.budo.dubbo.protocol.http.util.BudoSerializationUtil;
import org.budo.dubbo.protocol.http.view.render.AbstractViewRender;
import org.budo.dubbo.serialize.util.DubboSerializeUtil;

import com.alibaba.dubbo.common.logger.Logger;
import com.alibaba.dubbo.common.logger.LoggerFactory;
import com.alibaba.dubbo.common.serialize.Serialization;

/**
 * @author lmw
 */
public class DubboViewRender extends AbstractViewRender {
    private static final Logger log = LoggerFactory.getLogger(DubboViewRender.class);

    protected void addHeader(ProtocolResponse protocolResponse, Object result) {
        if (null == result) {
            log.error("#32 addHeader error, result is null, result=" + result);
            return;
        }

        Class<?> resultType = result.getClass();
        String resultTypeName = resultType.getName();

        protocolResponse.addHeader(Consts.HeaderKey.ResultType, resultTypeName);
    }

    @Override
    protected byte[] serializeResultValue(ProtocolRequest protocolRequest, Object resultValue) {
        Serialization serialization = BudoSerializationUtil.getSerialization(protocolRequest);
        return DubboSerializeUtil.serializeResultValue(serialization, resultValue, false);
    }
}